/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.anniversary.service.impl;

import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.anniversary.domain.Praise;
import me.zhengjie.modules.anniversary.repository.PraiseRepository;
import me.zhengjie.modules.anniversary.service.PraiseService;
import me.zhengjie.modules.anniversary.service.dto.PraiseDto;
import me.zhengjie.modules.anniversary.service.dto.PraiseQueryCriteria;
import me.zhengjie.modules.anniversary.service.mapstruct.PraiseMapper;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import me.zhengjie.utils.ValidationUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author wuhui
* @date 2020-09-24
**/
@Service
@RequiredArgsConstructor
public class PraiseServiceImpl implements PraiseService {

    private final PraiseRepository praiseRepository;
    private final PraiseMapper praiseMapper;

    @Override
    public Map<String,Object> queryAll(PraiseQueryCriteria criteria, Pageable pageable){
        Page<Praise> page = praiseRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(praiseMapper::toDto));
    }

    @Override
    public List<PraiseDto> queryAll(PraiseQueryCriteria criteria){
        return praiseMapper.toDto(praiseRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public PraiseDto findById(Long id) {
        Praise praise = praiseRepository.findById(id).orElseGet(Praise::new);
        ValidationUtil.isNull(praise.getId(),"Praise","id",id);
        return praiseMapper.toDto(praise);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public PraiseDto create(Praise resources) {
        return praiseMapper.toDto(praiseRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Praise resources) {
        Praise praise = praiseRepository.findById(resources.getId()).orElseGet(Praise::new);
        ValidationUtil.isNull( praise.getId(),"Praise","id",resources.getId());
        praise.copy(resources);
        praiseRepository.save(praise);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            praiseRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<PraiseDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (PraiseDto praise : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("发布的动态id", praise.getPraiseId());
            map.put("点赞人的id", praise.getUserId());
            map.put("点赞人姓名", praise.getPraiseName());
            map.put("点赞数", praise.getPraiseNum());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
